package com.kly.cashmall.module.profile;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.base.app_action.BaseActionHelper;
import com.kly.cashmall.bean.ProfileActionBean;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.profile.adapter.ProfileListAdapter;
import com.kly.cashmall.module.profile.presenter.ProfilePresenter;
import com.kly.cashmall.module.profile.presenter.ProfileViewer;
import com.kly.cashmall.utils.data.TextUtil;
import com.kly.cashmall.utils.function.ToastUtil;


/**
 * @author
 */
public class ProfileActivity extends BaseBarActivity implements ProfileViewer {

    @PresenterLifeCycle
    ProfilePresenter presenter = new ProfilePresenter(this);
    private RecyclerView mCertificationDataRecycle;
    private ProfileListAdapter mAdapter;

    public static Intent newIntent(Context context, String source) {
        Intent intent = new Intent(context, ProfileActivity.class);
        intent.putExtra("source", source);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        return intent;
    }

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_profile_view);
        setTitle(this.getResources().getString(R.string.profile));

        mCertificationDataRecycle = bindView(R.id.certification_data_recycle);
        mCertificationDataRecycle.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ProfileListAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mAdapter.getItem(position).isEnableToClick()) {
                BaseActionHelper.with(this).handleAction(mAdapter.getItem(position).getJumpUrl());
            } else {
                if (!TextUtil.isEmpty(mAdapter.getItem(position).getDisableText())) {
                    ToastUtil.showToast(mAdapter.getItem(position).getDisableText());
                }
            }
        });
        mCertificationDataRecycle.setAdapter(mAdapter);


    }

    @Override
    protected void loadData() {
    }

    @Override
    public void getProfileActionListSuccess(ProfileActionBean actionList) {
        bindView(R.id.profile_content, true);
        if (!TextUtils.isEmpty(actionList.getTips())) {
            bindText(R.id.profile_tip, actionList.getTips());
        }
        String tipsContent = actionList.getTipsContent();
        String tipEnd = "the corresponding loan products";
        SpannableString spannable = new SpannableString(tipsContent);
        if (tipsContent.contains(tipEnd)) {
            int start = tipsContent.indexOf(tipEnd);
            int end = start + tipEnd.length();
            spannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_FF5621)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        bindText(R.id.tips_details, spannable);
        mAdapter.setList(actionList.getActions());
        if (!TextUtil.isEmpty(getIntent().getStringExtra("source"))) {
            if (actionList.isAllActionComplete()) {
                BaseActionHelper.with(this).handleAction(actionList.getJumpUrl());
                finish();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getProfileActionList();
    }
}
